<template>
  <div>
    <van-icon
      :name='value===1 ? "good-job" : "good-job-o"'
      color='#e5645f'
      @click="onCollect"
      :loading='loading'
    />
  </div>
</template>

<script>
import { addLike, deleteLike } from '@/api/art'
export default {
  name: 'LikeArticle',
  components: {},
  props: {
    value: {
      type: Number,
      required: true
    },
    articleId: {
      type: [String, Number, Object],
      required: true
    }
  },
  data () {
    return {
      loading: false
    }
  },
  computed: {},
  watch: {},
  created () { },
  methods: {
    async onCollect () {
      this.loading = true
      try {
        let status = -1
        if (this.value === 1) {
          await deleteLike(this.articleId)
        } else {
          await addLike(this.articleId)
          status = 1
        }

        // 更新视图
        this.$emit('input', status)
        // 提示
        this.$toast.success(status === 1 ? '点赞成功' : '取消点赞')
      } catch (err) {
        this.$toast('失败请稍后重试')
      }
      this.loading = false
    }
  }
}
</script>

<style scoped>
</style>
